/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.containers.InternalFrameWithChart;

import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.containers.IChartPanelContainer;
import JFreeChartExtensions.exceptions.ExtendableChartPanelException;
import JFreeChartExtensions.interfaces.IPluginProvider;
import JFreeChartExtensions.storable.StorableChartFrame;
import java.awt.Cursor;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author Nobody
 */
public class InternalFrameWithChart extends JInternalFrame implements IChartPanelContainer {

    private String name;
    private ExtendableChartPanel chart;
    private JDesktopPane desktopPane;
    private JFrame parentFrame;

    public ExtendableChartPanel getChart() {
        return chart;
    }

    public void removeInternalFrame(JInternalFrame frame) {
        desktopPane.remove(frame);
    }

    public void addInternalFrame(JInternalFrame frame) {
        desktopPane.add(frame);
        desktopPane.setLayer(frame, 2);
        frame.setVisible(true);
    }

    public InternalFrameWithChart(JFrame parentFrame) throws InternalFrameWithChartException {
        this(parentFrame, null);
    }

    public InternalFrameWithChart(JFrame parentFrame, JDesktopPane pane) throws InternalFrameWithChartException {
        this.name = "Chart";
        this.desktopPane = pane;
        this.parentFrame = parentFrame;
        setTitle(name);
        //  Helper.getHelper().setIconForJInternalFrame(this);

        this.chart = new ExtendableChartPanel(name, this);
        setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setAutoscrolls(true);
        setClosable(true);
        setMaximizable(true);
        setIconifiable(true);
        setResizable(true);
        setBounds(70, 30, 450, 290);
        setCursor(Cursor.getDefaultCursor());
        setVisible(true);
    }

    public InternalFrameWithChart(JFrame parentFrame, JDesktopPane pane, StorableChartFrame chartObject) throws InternalFrameWithChartException {
        this(parentFrame, pane);
        name = chartObject.getName();
        setBounds(chartObject.getBounds());
        setLayer(chartObject.getLayer());
        chart.importStorableChart(chartObject);
        updateUI();
    }

    public void prepareChart(IPluginProvider pluginProvider) throws InternalFrameWithChartException {
        try {
            chart.launchExtensions(pluginProvider);
            chart.setPreferredSize(new java.awt.Dimension(500, 270));
            this.setContentPane(chart);
            this.setVisible(true);
        } catch (ExtendableChartPanelException ex) {
            throw new InternalFrameWithChartException("exception during initialization of chart", ex);
        }
    }

    public JFrame getParentFrame() {
        return parentFrame;
    }
}
